package service_book

import (
	"tools-api/src/common/db/database"
	"tools-api/src/jcj/model"

	"github.com/gin-gonic/gin"
	"xorm.io/xorm"
)

// AccountsGetList 收支账户列表
func AccountsGetList(uuid string, isList bool) []model.BookAccountsResult {
	list := make([]model.BookAccountsResult, 0)
	if isList {
		_ = database.JCJEngine.Table(model.BookAccounts{}).Alias("t").
			Join("LEFT", "book_detail d", "d.accounts_id = t.id and d.delete_mark=0").
			Where("t.uuid=? and t.delete_mark=0", uuid).
			Select("t.id, t.key, t.name, t.month_start, t.sort, t.def, case when COUNT(d.id) > 0 then 1 else 0 end as not_zero").
			GroupBy("t.id, t.key, t.name, t.sort, t.def").
			Asc("t.sort", "t.id").
			Find(&list)
	} else {
		_ = database.JCJEngine.Table(model.BookAccounts{}).Alias("t").
			Where("t.uuid=? and t.delete_mark=0", uuid).
			Select("t.id, t.key, t.name, t.month_start, t.sort, t.def").
			Asc("t.sort", "t.id").
			Find(&list)
	}
	hasDef := false
	for _, v := range list {
		if v.Def { // 查找是否有默认选中
			hasDef = true
			break
		}
	}
	result := make([]model.BookAccountsResult, 0)
	def := model.DefBookAccount
	def.Def = !hasDef                // 是否默认选中
	result = append(result, def)     // 默认账户
	result = append(result, list...) // 追加全部数据
	return result
}

// AccountsAdd 添加收支账户
func AccountsAdd(account model.BookAccounts) int64 {
	return account.Create()
}

// AccountsDelete 删除收支账户
func AccountsDelete(uuid string, id int64) error {
	return database.Session(database.JCJEngine.NewSession(), func(session *xorm.Session) error {
		// 删除收支账户
		err := model.BookAccounts{}.Delete(session, uuid, id)
		if err != nil {
			return err
		}
		// 删除该收支账户下的记账明细
		err = model.BookDetail{}.DeleteDetailByAccounts(session, uuid, id)
		return err
	})
}

// AccountsTransformDelete 转移数据 并 删除收支账户
func AccountsTransformDelete(uuid string, fromId, toId int64) error {
	return database.Session(database.JCJEngine.NewSession(), func(session *xorm.Session) error {
		// 转移数据
		err := model.BookDetail{}.TransformDetailByAccounts(session, uuid, fromId, toId)
		if err != nil {
			return err
		}
		// 删除收支账户
		err = model.BookAccounts{}.Delete(session, uuid, fromId)
		return err
	})
}

// AccountsSort 更新排序
func AccountsSort(uuid string, list [][]int64) error {
	return sort("book_accounts", uuid, list)
}

// AccountSetDefault 设置默认
func AccountSetDefault(uuid string, id int64) error {
	return model.BookAccounts{}.SetDefaultAccount(uuid, id)
}

// GetDefaultAccount 获取默认
func GetDefaultAccount(uuid string) gin.H {
	account := model.BookAccounts{}.GetDefaultAccount(uuid)
	if account.Id == 0 {
		def := model.DefBookAccount
		return gin.H{
			"id":          def.Id,
			"key":         def.Key,
			"name":        def.Name,
			"month_start": def.MonthStart,
		}
	} else {
		return gin.H{
			"id":          account.Id,
			"key":         account.Key,
			"name":        account.Name,
			"month_start": account.MonthStart,
		}
	}
}

// GetAccountById 根据id获取
func GetAccountById(id int64) model.BookAccounts {
	if id == 0 {
		def := model.DefBookAccount
		return model.BookAccounts{Id: def.Id, Key: def.Key, Name: def.Name, MonthStart: def.MonthStart}
	}
	account := model.BookAccounts{}.GetAccountById(id)
	if account.Id == 0 {
		account.Id = -1 // 方便判断
	}
	return account
}
